package com.qianwang.shopmsg.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.qianwang.shopmsg.common.AjaxResult;
import com.qianwang.shopmsg.common.Page;
import com.qianwang.shopmsg.common.PageData;
import com.qianwang.shopmsg.entity.Course;
import com.qianwang.shopmsg.exception.ParamValidateException;
import com.qianwang.shopmsg.param.CourseCreateParam;
import com.qianwang.shopmsg.param.CourseListParam;
import com.qianwang.shopmsg.service.course.CourseMsgService;

@Controller
@RequestMapping("course")
public class CourseMsgController extends BaseController{

	private final static Logger logger =  LoggerFactory.getLogger(CourseMsgController.class);
	
	@Autowired
	private CourseMsgService courseMsgService;
	/**
	 * 显示课程管理页面
	 * @return
	 */
	@RequestMapping("/showCourseInfos")
	public ModelAndView index(){
		ModelAndView mav = new ModelAndView("courseMsg/courseInfos");
		return mav;
	}
	
	@RequestMapping(value="listCourseInfos")
	@ResponseBody
	public PageData<Course> listCourseInfos(@RequestParam("rows") int rows,@RequestParam("page") int page,
			@RequestParam(value="courseListParam",required=false) String courseListParam){
		PageData<Course> data = null;
		try{
			CourseListParam listParam = JSONObject.parseObject(courseListParam, CourseListParam.class);
			Page pg = new Page(rows, page);
			data = new PageData<Course>();
			data.setTotal(courseMsgService.countCourse(listParam));
			data.setRows(courseMsgService.listCourseInfos(pg,listParam));
		}catch(Exception e){
			logger.error(e.getMessage(),e);
		}
		return data;
	}
	
	@RequestMapping(value = "/saveCourse", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveCourse(@RequestParam("paramJson") String paramJson) {
		try{
			CourseCreateParam courseParam = JSONObject.parseObject(paramJson, CourseCreateParam.class);
			courseMsgService.saveCourse(courseParam);
			return AjaxResult.success(null,"恭喜，操作成功");
		}catch(ParamValidateException e){
			logger.error(e.getMessage(),e);
			return AjaxResult.failed(e.getMessage());
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AjaxResult.failed("操作失败，请稍后重试");
		}
	}
	
	@RequestMapping(value = "/loadCourse", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult loadCourse(@RequestParam("id") long id) {
		try {
			Course course = courseMsgService.loadCourseById(id);
			return AjaxResult.success(course);
		} catch (ParamValidateException e) {
			logger.error(e.getMessage(),e);
			return AjaxResult.failed(e.getMessage());
		}
	}
	
	@RequestMapping(value = "/deleteCourse", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult deleteCourse(@RequestParam("id") long id) {
		try {
			courseMsgService.deleteCourse(id);
			return AjaxResult.success(null,"恭喜，操作成功");
		} catch (ParamValidateException e) {
			logger.error(e.getMessage(),e);
			return AjaxResult.failed(e.getMessage());
		}
	}
}
